#!/bin/bash
set -x
set -o errexit

sudo sysctl -a|grep max_map_count |grep 2621441 || (
 echo "vm.max_map_count=262144" | sudo tee /etc/sysctl.d/es.conf
 sudo sysctl --system
)
#sudo sysctl -w vm.max_map_count=262144

:<<EOF
curl -X PUT "localhost:9200/_settings" -H 'Content-Type: application/json' -d'
{
    "index" : {
        "number_of_replicas" : 0
    }
}
'
EOF

#切换版本
# 这些目录默认是elasticsearchy用户建的
rm -rf esdata/elasticsearch/*
rm -rf esdata/elasticsearch2/*
rm -rf esdata/esmon/*
rm -rf esdata/backup/*
mkdir -p esdata/{elasticsearch,elasticsearch2,esmon,backup}
sudo chmod 777 -R esdata

# docker ps -a |grep 'elasticsearch' && docker-compose rm -f
docker ps -a |grep 'elasticsearch' && (
    docker rm -f kibana || true
    docker rm -f kibanamon || true
    docker rm -f $(docker ps -a |grep 'elasticsearch'|awk '{print $1}')  || true
)

[ -f metricbeat-7.10.0-linux-x86_64.tar.gz ] || (
    #ftp://172.16.1.201/cluster/metricbeat-7.10.0-linux-x86_64.tar.gz
 wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-7.10.0-linux-x86_64.tar.gz -o metricbeat-7.10.0-linux-x86_64.tar.gz

 if [ -d tool/metricbeat-7.10.0-linux-x86_64 ]; then
  echo "binary ready"
 else
  tar xvzf metricbeat-7.10.0-linux-x86_64.tar.gz -C tool/.
  pushd 
    cp -rf metricbeat-7.10.0-linux-x86_64 metricbeat-7.10.0-linux-x86_64_2
  popd
 fi
)

chmod +x ./config/*.sh
chmod +x ./config/elasticsearch
chmod +x ./tool/*.sh
chmod +x ./tool/nc

if [ "$1" = "8m" ]; then
    echo "ES 7.8.0 With Standalone esmon"
    # https://www.elastic.co/guide/en/elasticsearch/reference/7.x/monitoring-production.html
    chmod +x ./config/*.sh
    sudo cp -f ./config/metricbeat.yml ./tool/metricbeat-7.10.0-linux-x86_64/.
    rm -f ./tool/metricbeat-7.10.0-linux-x86_64/modules.d/elasticsearch-xpack.yml.disabled
    sudo cp -f ./config/elasticsearch-xpack.yml ./tool/metricbeat-7.10.0-linux-x86_64/modules.d/.
    cp -f ./config/metricbeat.sh ./tool/metricbeat-7.10.0-linux-x86_64/

    docker-compose -f docker-compose_7.8.0_mon.yml up
    # read -p "Can metric beat start..."
    # docker exec -it elasticsearch bash -c "/opt/beat/metricbeat.sh&"
    # docker exec -it elasticsearch2 bash -c "/opt/beat/metricbeat.sh&"
elif [ "$1" = "8s" ]; then
    echo "ES 7.8.0 single es for tune"
    docker-compose -f docker-compose_7.8.0_s.yml up
elif [ "$1" = "8d" ]; then
    echo "ES 7.8.0 single es for tune and debug source code"
    docker-compose -f debug_7.8.0_s.yml up  
elif [ "$1" = "8" ]; then
    echo "ES 7.8.0"
    docker-compose -f docker-compose_7.8.0.yml up
elif [ "$1" = "8snap" ]; then
    echo "ES 7.8.0 es for snap restore"
    docker-compose -f docker-compose_7.8.0_snap.yml up
elif [ "$1" = "8hdfs" ]; then
    echo "ES 7.8.0 es for snap restore HDFS"
    #HDFS plugin
    if [ -f snaps/servers/pkg/repository-hdfs-7.8.0.zip ] && [ ! -f tool/repository-hdfs-7.8.0/plugin-security.policy ]; then
        mkdir -p tool/repository-hdfs-7.8.0
        unzip snaps/servers/pkg/repository-hdfs-7.8.0.zip -d tool/repository-hdfs-7.8.0
    elif [ ! -f tool/repository-hdfs-7.8.0/plugin-security.policy ]; then
        echo "HDFS　plugin must be ready"
        exit 1
    fi
    docker-compose -f docker-compose_7.8.0_snap_hdfs.yml up    
elif [ "$1" = "9" ]; then
    echo "ES 7.9.3"
    docker-compose -f docker-compose_7.9.3.yml up
else
# 7.7
:<<EOF
stack monitoring不完全好用，如下的异常。 按照官方的一些资料也没有解决？？ TODO
stacktrace": ["org.elasticsearch.xpack.monitoring.exporter.ExportException: failed to add documents to export bulks",
"at org.elasticsearch.xpack.monitoring.exporter.ExportBulk$Compound.doAdd(ExportBulk.java:91) ~[?:?]",
"at org.elasticsearch.xpack.monitoring.exporter.ExportBulk.add(ExportBulk.java:52) ~[?:?]",
EOF
    echo "ES 7.7.1"
    docker-compose -f docker-compose_7.7.1.yml up
fi


